package com.voicechat.firstmet;

import com.tencent.ilivesdk.core.ILiveLoginManager;

import java.util.LinkedList;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet
 * @ClassName: IMStatusObservable
 * @Description: 聊天状态监听类
 * @Author: ljp
 * @CreateDate: 2018/11/8 14:15
 * @Version: 1.0
 */
public class IMStatusObservable implements ILiveLoginManager.TILVBStatusListener{
    // 消息监听链表
    private LinkedList<ILiveLoginManager.TILVBStatusListener> listObservers = new LinkedList<>();
    // 句柄
    private static IMStatusObservable instance;


    public static IMStatusObservable getInstance() {
        if (null == instance) {
            synchronized (IMStatusObservable.class) {
                if (null == instance) {
                    instance = new IMStatusObservable();
                }
            }
        }
        return instance;
    }


    // 添加观察者
    public void addObserver(ILiveLoginManager.TILVBStatusListener listener) {
        if (!listObservers.contains(listener)) {
            listObservers.add(listener);
        }
    }

    // 移除观察者
    public void deleteObserver(ILiveLoginManager.TILVBStatusListener listener) {
        listObservers.remove(listener);
    }

    // 获取观察者数量
    public int getObserverCount() {
        return listObservers.size();
    }

    @Override
    public void onForceOffline(int error, String message) {
        // 拷贝链表
        LinkedList<ILiveLoginManager.TILVBStatusListener> tmpList = new LinkedList<>(listObservers);
        for (ILiveLoginManager.TILVBStatusListener listener : tmpList) {
            listener.onForceOffline(error, message);
        }
    }

}
